package com.bd.scm.module.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bd.scm.dao.mapper.user.LeaveManagementDao;
import com.bd.scm.dao.mapper.user.PersonnelFilesDao;
import com.bd.scm.domain.dto.user.PersonnelFilesDto;
import com.bd.scm.domain.models.user.PersonnelFiles;
import com.bd.scm.module.user.service.ILeaveManagementService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LeaveManagementServiceImpl implements ILeaveManagementService {
    @Resource
    private LeaveManagementDao leaveManagementDao;
    @Resource
    private PersonnelFilesDao personnelFilesDao;

    @Override
    public IPage<PersonnelFiles> getLeavePersonList(PersonnelFilesDto personnelFilesDto) {
        Page<PersonnelFiles> page = new Page<PersonnelFiles>(personnelFilesDto.getCurrent(),personnelFilesDto.getPageSize());
        return leaveManagementDao.getLeavePersonList(page,personnelFilesDto);
    }

    @Override
    public String recoveryOccupation(List<PersonnelFilesDto>  list, String clientIp) {
        String r = null;
        for (PersonnelFilesDto personnelFilesDto : list) {
            personnelFilesDto.setLogIp(clientIp);
            String user_lname = personnelFilesDto.getUserLname();
            String user_no = personnelFilesDto.getUserNo();
            personnelFilesDto.setLogBz("姓名:"+user_lname+"  工号:"+user_no);
            int i=leaveManagementDao.recoveryOccupation(personnelFilesDto);
            if(i>0){
                leaveManagementDao.insertWtPublicByLeave(personnelFilesDto);
                leaveManagementDao.insertWtUserLogByLeave(personnelFilesDto);
            }else{
                r="复职失败";
            }

        }
        return r;
    }

    @Override
    public String transferPerson(List<PersonnelFilesDto> list, String clientIp) {
        String r = null;
        for (PersonnelFilesDto personnelFilesDto : list) {
            personnelFilesDto.setLogIp(clientIp);
            PersonnelFilesDto depInfo= personnelFilesDao.getDepInfoNew(personnelFilesDto);
            personnelFilesDto.setDepNo(depInfo.getDepNo());
            personnelFilesDto.setDepName(depInfo.getDepName());
            PersonnelFilesDto j = personnelFilesDao.queryPersonById(personnelFilesDto);
            String user_lname = j.getUserLname();
            String user_no = j.getUserNo();
            String depSerial = personnelFilesDao.getDepSerial(personnelFilesDto);
            personnelFilesDto.setDepSerial(depSerial);
            personnelFilesDao.realUserRemove(personnelFilesDto);
            int i=personnelFilesDao.transferUser(personnelFilesDto);
            if(i>0){
                personnelFilesDto.setLogBz("姓名"+user_lname+"工号:"+user_no+"从"+personnelFilesDto.getDepName()+"调动到"+depInfo.getDepSerialNew());
                personnelFilesDao.insertTransferWt(personnelFilesDto);
                personnelFilesDao.insertTransferLog(personnelFilesDto);
                personnelFilesDto.setLogBz("姓名:"+user_lname+"  工号:"+user_no);
                int a=leaveManagementDao.recoveryOccupation(personnelFilesDto);
                if(a>0){
                    leaveManagementDao.insertWtPublicByLeave(personnelFilesDto);
                    leaveManagementDao.insertWtUserLogByLeave(personnelFilesDto);
                }else{
                    r="复职失败";
                }
            }else{
                r+=user_lname+"调动失败";
            }
        }
        return r;
    }
}
